<?php

namespace Admin\Controller;

use Org\Util\ArrayToolkit;

class UserController extends AdminController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = D('Common/User');
    }

    public function index()
    {
        // 匿名函数请求
        $this->IS_POST(function () {
            $map = M('User')->create() ?: array();
            $data = $this->model->where($map)->field('id,username,nickname,email,reg_ip,reg_time,status')->findPage();
            foreach ($data['data'] as &$row) {
                $row['reg_time'] = date('Y-m-d H:i', $row['reg_time']);
            }
            $data['code'] = 0;
            $this->ajaxReturn($data);
        });
        $this->_web('title', '用户信息');
        $this->_web('panel_title', '用户信息');
        $listData = array(
            'url' => U(''),
            'toolbar' => '#barDemo',
            'toolbarTpl' => 'toolbar',
            'page' => true,
        );
        $listData['cols'][] = array('type' => 'checkbox', 'fixed' => 'left');
        $listData['cols'][] = array('field' => 'id', 'title' => 'ID', 'sort' => true);
        $listData['cols'][] = array('field' => 'username', 'align' => 'left', 'title' => '用户名', 'width' => '15%');
        $listData['cols'][] = array('field' => 'nickname', 'title' => '昵称', 'width' => '15%');
        $listData['cols'][] = array('field' => 'email', 'title' => 'email', 'width' => '10%');
        $listData['cols'][] = array('field' => 'reg_ip', 'title' => '注册IP', 'width' => '10%');
        $listData['cols'][] = array('field' => 'reg_time', 'title' => '注册时间', 'width' => '15%');
        $listData['cols'][] = array('field' => 'status_text', 'title' => '状态', 'width' => 86);
        $listData['cols'][] = array('fixed' => 'right', 'width' => '220', 'title' => '操作', 'toolbar' => '#rowBar');
        $this->displayList($listData);
    }

    public function add()
    {
        $id = I('id');
        $this->IS_POST(function () use ($id) {
            $data = $this->model->create();
            $group_access = D('UserGroupAccess')->create();
            if (!$data) {
                $this->error($this->model->getError());
            }
            $data['UserGroupAccess'] = $group_access;
            if ($this->model->relation(true)->in2replace($data, array('id' => $id)) !== false) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        });
        $groups = D('UserGroup')->where(array('status' => 1))->select();
        $groups = ArrayToolkit::KeyAndValue($groups, 'id', 'title');
        if ($id) {
            $info = $this->model->relation(true)->find($id);
            $this->assign('info', $info);
        }
        $this->assign('groups', $groups);
        $this->display();
    }

    public function group()
    {
        $this->_web('title', '角色管理');
        $this->_web('panel_title', '角色管理');
        $this->IS_POST(function () {
            $model = D('Common/UserGroup');
            $list = $model->field('id,title,code,status')->findPage();
            $this->ajaxReturn($list);
        });
        $listData = array(
            'url' => U(''),
            'toolbarTpl' => 'grouptool',
            'toolbar' => '#barDemo',
            'page' => true,
        );
        $listData['cols'][] = array('type' => 'checkbox', 'fixed' => 'left');
        $listData['cols'][] = array('field' => 'id', 'title' => 'ID', 'sort' => true);
        $listData['cols'][] = array('field' => 'title', 'align' => 'left', 'title' => '角色名', 'width' => '20%');
        $listData['cols'][] = array('field' => 'code', 'title' => '角色代码', 'width' => '15%');
        $listData['cols'][] = array('field' => 'status_text', 'title' => '角色状态', 'width' => '15%');
        $listData['cols'][] = array('fixed' => 'right', width => '220', 'title' => '操作', 'toolbar' => '#rowBar');
        $this->displayList($listData, 'group');
    }

    public function groupAdd()
    {
        $model = D('UserGroup');
        $id = I('id');
        $this->IS_POST(function () use($model, $id) {
            $data = $model->create();
            if(!$data) {
                $this->error($model->getError());
            }else if($model->in2replace($data, array('id'=>$id)) !== false) {
                $this->success('操作成功');
            }else {
                $this->error('操作失败');
            }
        });
        if($id) {
            $info = $model->find($id);
            if(!$info) $this->error('没有该角色');
            $this->assign('info', $info);
        }
        $this->display();
    }
    public function groupDel() {
        $this->model = D('UserGroup');
        parent::del();
    }
    public function groupView() {
        $id = I('id');
        $model = D('UserGroup');
        $info = $model->find($id);
        if(!$info)
            $this->error('没有该角色');
        $this->assign('info', $info);
        $this->display();
    }

    public function view()
    {
        $id = I('id');
        $info = D('UserView')->find($id);
        if (!$info) {
            $this->error('没有该用户');
        }
        $this->assign('info', $info);
        $this->display();
    }

    public function groupAuth()
    {
        $model = D('UserGroup');
        $lists = D('UserRule')->field('title name,pid,id')->select();
        $id = I('id');
        $info = $model->find($id);
        if (!$info)
            $this->error('没有该角色');
        $ex = array_filter(explode(',', $info['rules']));
        foreach ($lists as &$row) {
            if (in_array($row['id'], $ex))
                $row['checked'] = true;
        }
        array_unshift($lists, array(
            'id' => 0, 'pid' => null, 'name' => '权限管理', 'isParent' => true, 'open' => true
        ));

        $this->assign('info', $info);
        $this->assign('lists', $lists);
        $this->display();
    }
    public function online() {
        $this->_web('title', '在线用户');
        $this->_web('panel_title', '在线用户');
        $this->IS_POST(function () {
            $model = D('UserSessionView');
            $data = $model->where(array('UserSession.type'=>'login'))->findPage();
            foreach ($data['data'] as &$row) {
                $row['start_time'] = smart_time($row['start_time']);
                $row['expire'] = date('Y-m-d H:i:s', $row['expire']);
            }
            $this->ajaxReturn($data);
        });
        $listData = array(
            'url' => U(''),
            'toolbarTpl' => 'online_tool',
            'page' => true,
        );
//        $listData['cols'][] = array('type' => 'checkbox', 'fixed' => 'left');
        $listData['cols'][] = array('field' => 'id', 'title' => 'ID', 'sort' => true);
        $listData['cols'][] = array('field' => 'username', 'align' => 'left', 'title' => '用户名', 'width' => '15%');
        $listData['cols'][] = array('field' => 'nickname', 'title' => '昵称', 'width' => '15%');
        $listData['cols'][] = array('field' => 'start_time', 'title' => '登录时间', 'width' => '15%');
        $listData['cols'][] = array('field' => 'expire', 'title' => '过期时间', 'width' => '15%');
        $listData['cols'][] = array('fixed' => 'right', 'title' => '操作', 'toolbar' => '#rowBar');
        $this->displayList($listData);
    }

    /**
     * 立即执行强制下线
     * @param string $uid
     */
    public function outline($uid = '') {
        if(D('UserSession')->outline($uid,'login')) {
            $this->success('该用户已被强制下线', $_SERVER['HTTP_REFERER']);
        }
        $this->error('未知错误.');
    }

    public function _beging_del($ids, &$url)
    {
//        $url = false;
    }
}